
#include  <msp430x54xA.h>


enum I2C_Status
{
  None,
  I2cDone,
  I2cWriteFail,
  I2cReadFail,
  I2cAckFail,
  NoRespondence,
  I2cEEPROM_RWFail,
  I2cSCLHoldTimeOut
};


///////////////////////////////////////////////////////////////////////
/* IICx Active Status Bits , (G_IICx_Active_Status)
 * unsigned char (8 bits)   */
#define IIC_START_TRX                (0x01u)  //IIC transmitting and then Receiving action start
#define IIC_START_TX                 (0x02u)  //IIC transmitting action start
#define IIC_TX_FINISH                (0x04u)  //IIC transmitting Finish,  (;set by interrupt)
#define IIC_TX_NACK                  (0x08u)  //IIC transmitting Fail or nack
#define IIC_START_RX                 (0x10u)  //IIC Receiving action start 
#define IIC_RX_FINISH                (0x20u)  //IIC Receiving Finish,  (;set by interrupt)
#define IIC_RX_NACK                  (0x40u)  //IIC Receiving Fail or nack
#define IIC_NO_STOP_FLAG             (0x80u)  //IIC transmitting send No stop signal

///////////////////////////////////////////////////////////////////////
/* IICx Control Bits , (G_IICx_Control_Bits)
 * unsigned char (8 bits)   */
//#define IIC_START_TRX                (0x01u)  //IIC transmitting and then Receiving action start
//#define IIC_START_TX                 (0x02u)  //IIC transmitting action start
//#define IIC_TX_FINISH                (0x04u)  //IIC transmitting Finish
//#define IIC_TX_FAIL                  (0x08u)  //IIC transmitting Fail or nack
//#define IIC_START_RX                 (0x10u)  //IIC Receiving action start 
//#define IIC_RX_FINISH                (0x20u)  //IIC Receiving action start
//#define IIC_RX_FAIL                  (0x40u)  //IIC Receiving Finish
//#define IIC_NO_STOP_FLAG             (0x80u)  //IIC transmitting send No stop signal



//extern unsigned char G_IIC_0_Active_Status;
//extern unsigned char G_IIC_1_Active_Status;
//extern unsigned char G_IIC_2_Active_Status;

//extern unsigned char G_IIC_0_Control_Bits;
//extern unsigned char G_IIC_1_Control_Bits;
//extern unsigned char G_IIC_2_Control_Bits;


//#define UCB0_START_TX                (0x0001u)  //UCB0 transmitting action start
//#define UCB0_TRX_STOP                (0x0002u)  //UCB0 transmitting or Receiving action stop
//#define UCB0_NO_STOP_FLAG            (0x0004u)  //UCB0 transmitting send No stop signal
//#define UCB0__                (0x0008u)
//#define UCB1_START_TX                (0x0010u)  //UCB1 transmitting action start
//#define UCB1_TRX_STOP                (0x0020u)  //UCB1 transmitting or Receiving action stop
//#define UCB1_NO_STOP_FLAG            (0x0040u)  //UCB1 transmitting send No stop signal
//#define UCB1__                (0x0080u)
//#define UCB2_START_TX                (0x0100u)  //UCB2 transmitting action start
//#define UCB2_TRX_STOP                (0x0200u)  //UCB2 transmitting or Receiving action stop
//#define UCB2_NO_STOP_FLAG            (0x0400u)  //UCB2 transmitting send No stop signal
//#define UCB2__                (0x0800u)
//#define UCB3_START_TX                (0x1000u)  //UCB3 transmitting action start
//#define UCB3_TRX_STOP                (0x2000u)  //UCB3 transmitting or Receiving action stop
//#define UCB3_NO_STOP_FLAG            (0x4000u)  //UCB3 transmitting send No stop signal
//#define UCB3__                (0x8000u)
//
//
//extern unsigned int G_IIC_Active_Status;

extern const unsigned char CRC8LookUp[];
//unsigned char Calculate_CRC8(unsigned char CRC_Pre_Result, unsigned char DataByte);

/////////////////////////////////////
//Assign I2C Master  Define
#define TX_DataBuffer           10
#define RX_DataBuffer           17
#define DEVICE_ADDRESS          0x0B  // for master address
#define WAIT_FOR_FINISH_COUNT   100    // time based 10 us; 1 byte


/////////////////////////////////////
//Assign I2C pins to USCI_B0
//P3.1:SDA, P3.2:SCL
//////////////////////////////////////////
void Init_USCI_B0_IIC();
unsigned char MasterIIC_WRead_Bytes_UCB0(unsigned char Cmd, unsigned char *PRxArray,unsigned char RxBytes, unsigned char isPECCheck);
unsigned char MasterIIC_Write_Bytes_UCB0(unsigned char *PTxArray, unsigned char TxBytes, unsigned char isPECCheck);
unsigned char MasterIIC_Write_Read_Bytes_UCB0(unsigned char *PTxArray, unsigned char TxBytes, unsigned char *PRxArray,unsigned char RxBytes, unsigned char isPECCheck);



///////////////////////////////////////////
//Assign I2C pins to USCI_B1
//P3.7:SDA, P5.4:SCL
//////////////////////////////////////////

void Init_USCI_B1_IIC();
unsigned char MasterIIC_WRead_Bytes_UCB1(unsigned char Cmd, unsigned char *PRxArray,unsigned char RxBytes, unsigned char isPECCheck);
unsigned char MasterIIC_Write_Bytes_UCB1(unsigned char *PTxArray, unsigned char TxBytes, unsigned char isPECCheck);

///////////////////////////////////////////
//Assign I2C pins to USCI_B2
//P9.1:SDA, P9.2:SCL
//////////////////////////////////////////

void Init_USCI_B2_IIC();
unsigned char MasterIIC_WRead_Bytes_UCB2(unsigned char Cmd, unsigned char *PRxArray,unsigned char RxBytes, unsigned char isPECCheck);
unsigned char MasterIIC_Write_Bytes_UCB2(unsigned char *PTxArray, unsigned char TxBytes, unsigned char isPECCheck);


///////////////////////////////////////////
//Assign I2C pins to USCI_B3
//Assign I2C Slave  Define
//P10.1:SDA, P10.2:SCL
#define Default_Slave_ADDRESS           0x0B  // for slave Address
//#define RETRY_COUNT           3
#define SLAVE_MAX_TRANSMIT_BUFFER       44
#define SLAVE_MAX_RECEIVE_BUFFER        8

void Init_USCI_B3_IIC(char ownAddress);
void Init_USCI_B3_IIC_HW();

////////////////////////////////////////////
////extern volatile unsigned char RxBuffer_UCB3[];
//
//extern void getSlaveTransmitData(unsigned char command);
//extern unsigned char TxBuffer_UCB3[MAX_TRANSMIT_COUNT];       // Allocate XXX byte of RAM
//
//#define MAX_TX_Buffer_Count 5
//#define MAX_TX_Buffer_Size 6
//static unsigned char TX_Buffer_Index;

//unsigned char TxBufferData[];



////////////////////////////////////////////////////////////////////////////
//EEPROM Control configuration 
// P2.5 WP control
////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
#define EEPROM_ADDRESS         0x50 // FOR Page 0 Address

#define EEPROM_WP_PORT_OUT      P2OUT
#define EEPROM_WP_PORT_DIR      P2DIR
#define EEPROM_WP_PORT_SEL      P2SEL
#define EEPROM_WP               BIT5

void Init_EEPROM();
void setEEPROMRewritable(char enable);
unsigned char EEPROM_Write(unsigned int nAddress, unsigned char *PTxData, unsigned char dataCount);
unsigned char EEPROM_Read(unsigned int nAddress, unsigned char *PRxData, unsigned char dataCount);
//
///*
////////////////////////////////////////////////
//// For EEPROM 8K, BR24S08
////////////////////////////////////////////////
//enum EEPROM_PageNum{
//  _P0,
//  _P1,
//  _P2,
//  _P3  
//};
//void Init_EEPROM(char PageNum);
//void setEEPROMRewritable(char enable);
//char EEPROM_Write(char nAddress, unsigned char *PTxData, char dataCount);
//char EEPROM_Read(char nAddress, unsigned char *PRxData, char dataCount);
//*/

//
/////////////////////////////////////////////
////Assign I2C pins to USCI_B3
////P10.1:SDA, P10.2:SCL
////////////////////////////////////////////
////extern volatile unsigned char RxBuffer_UCB3[];
//#define MAX_TRANSMIT_COUNT 100
//#define MAX_RECEIVE_COUNT 32
//
//void Init_USCI_B3_IIC(char ownAddress);
//extern void getSlaveTransmitData(unsigned char command);
//extern unsigned char TxBuffer_UCB3[MAX_TRANSMIT_COUNT];       // Allocate XXX byte of RAM
//
//#define MAX_TX_Buffer_Count 5
//#define MAX_TX_Buffer_Size 6
//static unsigned char TX_Buffer_Index;
//
////unsigned char TxBufferData[];
//
//
//
//
//char SMBUS0_WriteWord_PEC(unsigned char command, unsigned char *WriteData);
//char SMBUS0_ReadWord_PEC(unsigned char command, unsigned char *ReadData);
//char SMBUS0_WriteBlock_PEC(unsigned char command, unsigned char *WriteData, unsigned char DataLength );
//char SMBUS0_ReadBlock_PEC(unsigned char command, unsigned char *ReadData, unsigned char *DataLength);
//
//
//char SMBUS1_WriteWord_PEC(unsigned char command, unsigned char *WriteData);
//char SMBUS1_ReadWord_PEC(unsigned char command, unsigned char *ReadData);
//char SMBUS1_WriteBlock_PEC(unsigned char command, unsigned char *WriteData, unsigned char DataLength );
//char SMBUS1_ReadBlock_PEC(unsigned char command, unsigned char *ReadData, unsigned char *DataLength);
//
//char SMBUS2_WriteWord_PEC(unsigned char command, unsigned char *WriteData);
//char SMBUS2_ReadWord_PEC(unsigned char command, unsigned char *ReadData);
//char SMBUS2_WriteBlock_PEC(unsigned char command, unsigned char *WriteData, unsigned char DataLength );
//char SMBUS2_ReadBlock_PEC(unsigned char command, unsigned char *ReadData, unsigned char *DataLength);
//
